package com.property.manage.service.resource;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guardchina.framework.mybatis.model.CustomPage;
import com.guardchina.framework.util.exception.BusinessException;
import com.property.manage.dao.resource.BuildingMapper;
import com.property.manage.enums.WalletType;
import com.property.manage.model.owner.OwnerEntity;
import com.property.manage.model.resource.Building;
import com.property.manage.model.resource.BuildingVo;
import com.property.manage.model.resource.ProBuildingVo;
import com.property.manage.model.village.Village;
import com.property.manage.service.flowingWater.WalletService;

import com.property.manage.service.owner.OwnerService;
import com.property.manage.service.village.VillageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * 描述
 *
 * @author lwb
 * @since 2020/7/15
 */
@Component
@Slf4j
public class BuildingService {
    @Autowired
    private BuildingMapper buildingMapper;
    @Autowired
    private VillageService villageService;
    @Autowired
    private WalletService walletService;

    @Autowired
    private OwnerService ownerService;

    public BuildingVo findBuilding(Integer id) {
        return buildingMapper.queryById(id);
    }

    public CustomPage<BuildingVo> buildingPage(Long pageNo, Long pageSize, Integer villageId,
                                             Integer building, Integer unit,
                                             String roomNumber) {
        Page<BuildingVo> page = new Page<>(pageNo, pageSize);
        IPage<BuildingVo> village = buildingMapper.getBuilding(page, villageId, building, unit, roomNumber);
        return new CustomPage<>(village);
    }

    public List<Integer> getBuildingId(Integer building, Integer unit,
                                       String roomNumber){
        return buildingMapper.getBuildingId(building,unit,roomNumber);
    }
    /**
     * 拼接楼栋信息
     * @param id
     * @return
     */
    public String findAddress(Integer id){
        Building building = this.findBuilding(id);
        if (building==null){
            throw new BusinessException("楼栋信息错误",String.format("楼栋id[%s]",id));
        }
        Village village = villageService.findVillage(building.getVillageId());
        if (village==null){
            throw new BusinessException("小区信息错误",String.format("小区id[%s]",building.getVillageId()));
        }

        return findAddress(village, building);
    }

    public String findAddress(Building building){
        return findAddress(null, building);
    }

    private String findAddress(Village village, Building building){
        StringBuilder sb=new StringBuilder();
        if(village != null){
            sb.append(village.getName()).append("-");
        }

        sb.append(building.getBuilding()).append("-").append(building.getUnit()).append("-").append(building.getRoomNumber());

        return sb.toString();
    }

    @Transactional(rollbackFor = Exception.class)
    public void add(Integer villageId, Integer building, Integer status, Integer unit, String roomNumber,
                    BigDecimal square,String discount) {
        Building buildingVo = new Building();
        buildingVo.setBuilding(building);
        buildingVo.setRoomNumber(roomNumber);
        buildingVo.setSquare(square);
        buildingVo.setVillageId(villageId);
        buildingVo.setStatus(status);
        buildingVo.setUnit(unit);
        String valueOf = String.valueOf(roomNumber);
        String s = valueOf.substring(valueOf.length() - 2,valueOf.length() - 1);
        buildingVo.setType(s);
        buildingVo.setDiscount(discount);
        buildingMapper.insert(buildingVo);
        Integer id = buildingVo.getId();
        walletService.createWallet(id,villageId);
    }

    public void update(Integer id, Integer villageId, Integer building, Integer status, Integer unit, String roomNumber,
                       BigDecimal square,String discount) {
        UpdateWrapper<Building> wrapper = new UpdateWrapper<>();
        wrapper.set("village_id", villageId);
        wrapper.set("building", building);
        wrapper.set("unit", unit);
        wrapper.set("status", status);
        wrapper.set("square", square);
        wrapper.set("room_number", roomNumber);
        String valueOf = String.valueOf(roomNumber);
        String s = valueOf.substring(valueOf.length() - 1);
        wrapper.set("type", s);
        wrapper.set("discount", discount);
        wrapper.eq("id", id);
        buildingMapper.update(null, wrapper);
    }
    public void delete(Integer id) {
        buildingMapper.deleteById(id);
    }

    /**
     * 查找楼栋信息
     * @param villageId
     * @param building
     * @param unit
     * @param roomNumber
     * @return
     */
    public Building findOne(Integer villageId, Integer building, Integer unit, String roomNumber){
        QueryWrapper<Building> wrapper = new QueryWrapper<>();
        wrapper.eq("village_id", villageId);
        wrapper.eq("building", building);
        wrapper.eq("unit", unit);
        wrapper.eq("room_number", roomNumber);
        return   buildingMapper.selectOne(wrapper);
    }

    public Integer getBuildingTotal(@Param("villageId") Integer villageId){
        return buildingMapper.getBuildingTotal(villageId);
    }

    public List<Building> getBuildings(Integer villageId){
        QueryWrapper<Building> wrapper = new QueryWrapper<>();
        wrapper.eq("village_id", villageId);

        return buildingMapper.selectList(wrapper);
    }
    /**
     * 修改折扣
     * @param id
     * @param discount
     */
    public void updateDiscount(Integer id,String discount){
        UpdateWrapper<Building> up=new UpdateWrapper<>();
        up.set("discount",discount);
        up.eq("id",id);
        buildingMapper.update(null,up);
    }

    /**
     * 预收页面回显数据
     * @param id
     * @return
     */
    public ProBuildingVo findProBuildingVo(Integer id, WalletType walletType){

        ProBuildingVo proBuildingVo = new ProBuildingVo();
        OwnerEntity ownerEntity = ownerService.findByBuildingId(id);
        proBuildingVo.setOwnerName(Optional.ofNullable(ownerEntity).map(OwnerEntity::getOwnerName).orElse(""));

        proBuildingVo.setMoney(walletService.findBalance(id, walletType));

        return proBuildingVo;


        /*ProBuildingVo proBuildingVo = buildingMapper.queryPreBuildingVo(id);
        if (proBuildingVo==null){
            return null;
        }
        BigDecimal propertyMoney = proBuildingVo.getPropertyMoney();
        BigDecimal square = proBuildingVo.getSquare();
        BigDecimal decimal = propertyMoney.multiply(square);
        proBuildingVo.setMoney(decimal);
        return proBuildingVo;*/
    }

    public List<Integer> selectBuilding(Integer villageId){
        return buildingMapper.selectBuilding(villageId);
    }
    public List<Integer> selectByVillageId(Integer villageId){
        List<Integer> integers = buildingMapper.selectByVillageId(villageId);
        if (integers.isEmpty()){
            return Collections.emptyList();
        }
        return integers;
    }
    public List<Integer> selectBuilding(Integer villageId,Integer building){
        return buildingMapper.selectId(villageId,building);
    }
}
